#include "timedmultisetproxy.h"

#include "../utils/expressionengine.h"
#include "boolproxy.h"
#include "numberproxy.h"

#include <QtScript/QScriptEngine>

using namespace core;
using namespace wfnet;

const QString TimedMultisetProxy::NAME =
    TimedMultisetProxy::staticMetaObject.className();

TimedMultisetProxy::TimedMultisetProxy(QObject* parent) : AbstractTypeProxy(parent),
    m_value()
{

}

TimedMultisetProxy::TimedMultisetProxy(const TimedMultisetProxy& proxy) :
    AbstractTypeProxy(proxy.parent()),
    m_value(proxy.m_value)
{
    m_engine = proxy.m_engine;
}

TimedMultisetProxy::~TimedMultisetProxy()
{

}

void TimedMultisetProxy::setValue(const QVariant& value)
{
    m_value = TimedMultiset(m_value.typeOf(), value.toString());
}

const core::Type* TimedMultisetProxy::getValue() const
{
    return &m_value;
}

QScriptValue TimedMultisetProxy::add(const QScriptValue& other)
{
    Q_ASSERT(m_engine != 0);

    TimedMultisetProxy* _other = qobject_cast<TimedMultisetProxy* >(other.toQObject());

    TimedMultisetProxy* ret = new TimedMultisetProxy(*this);
    ExpressionEngine::getInstance()->registerProxyInstance(ret);

    if (_other != 0)
    {
        ret->m_value = m_value + _other->m_value;
    }
    else
    {
        ret->m_value = TimedMultiset(m_value.typeOf());
    }

    return m_engine->newQObject(ret);
}

QScriptValue TimedMultisetProxy::sub(const QScriptValue& other)
{
   Q_ASSERT(m_engine != 0);

    TimedMultisetProxy* _other = qobject_cast<TimedMultisetProxy* >(other.toQObject());

    TimedMultisetProxy* ret = new TimedMultisetProxy(*this);
    ExpressionEngine::getInstance()->registerProxyInstance(ret);

    if (_other != 0)
    {
        ret->m_value = m_value - _other->m_value;
    }
    else
    {
        ret->m_value = TimedMultiset(m_value.typeOf());
    }

    return m_engine->newQObject(ret);
}

QScriptValue TimedMultisetProxy::mul(const QScriptValue& other)
{
    Q_ASSERT(m_engine != 0);

    NumberProxy* _other = qobject_cast<NumberProxy* >(other.toQObject());

    TimedMultisetProxy* ret = new TimedMultisetProxy(*this);
    ExpressionEngine::getInstance()->registerProxyInstance(ret);

    if (_other != 0)
    {
        const ValueType* type = dynamic_cast<const ValueType*>(
            _other->getValue());
        ret->m_value = m_value * (type != 0 ? type->getValue().toUInt() : 1);
    }
    else
    {
        ret->m_value = TimedMultiset(m_value.typeOf());
    }

    return m_engine->newQObject(ret);
}

QScriptValue TimedMultisetProxy::lessOrEqual(const QScriptValue& other)
{
    Q_ASSERT(m_engine != 0);

    TimedMultisetProxy* _other = qobject_cast<TimedMultisetProxy* >(other.toQObject());

    BoolProxy* ret = new BoolProxy();
    ExpressionEngine::getInstance()->registerProxyInstance(ret);
    ret->setEngine(m_engine);

    if (_other != 0)
    {
        const bool value = m_value <= _other->m_value;
        ret->setValue(value);
    }
    else
    {
        ret->setValue(false);
    }

    return m_engine->newQObject(ret);
}

void TimedMultisetProxy::setIdentity(const core::Type& type)
{
    m_value.setIdentity(type);
}
